//                        date: [[20120714190007]]
//                        Author: Ahmed Fathi Hamouda Abd El Hafez
using System;
using System.ComponentModel;
using System.Reflection;
using System.Data;
using System.Diagnostics;
using StoresManagementDataLayer;
using System.Collections.Generic;


namespace StoresManagementBusinessLayer
{

    /// <summary>
    ///  Business Object OF Table Products
    /// </summary>

    public partial class BLProduct : INotifyPropertyChanged
    {
        #region Fields
        private BLProduct.BusinessRelatedCollections f_RelatedCollections = null;
        #endregion

        #region properties
        /// <summary>
        /// RelatedCollections of BLProduct
        /// </summary>
        [Browsable(false)]
        public BLProduct.BusinessRelatedCollections RelatedCollections
        {
            get
            {
                if (this.f_RelatedCollections == null)
                {
                    this.f_RelatedCollections = new BusinessRelatedCollections(this);
                }
                return f_RelatedCollections;
            }
            set
            {
                f_RelatedCollections = value;
            }
        }
        #endregion

        #region methods
        /// <summary>
        /// test that object dosn't have any relations and can be deleted
        /// </summary>
        public bool IsDeleteReady()
        {
            bool hasRelations = false;
            hasRelations = hasRelations || (this.RelatedCollections.CollectionBillsDetailByProduct_ID.Count > 0);
            
            hasRelations = hasRelations || (this.RelatedCollections.CollectionDealer_ProductByProduct_ID.Count > 0);
            
            hasRelations = hasRelations || (this.RelatedCollections.CollectionOfferByProduct_ID.Count > 0);
            
            hasRelations = hasRelations || (this.RelatedCollections.CollectionProduct_MeasureUnitByProduct_ID.Count > 0);
            
            hasRelations = hasRelations || (this.RelatedCollections.CollectionStore_ProductByProduct_ID.Count > 0);
            return !hasRelations;
        }
        #endregion

        #region RelatedCollections
        /// <summary>
        /// contains properties of the Related Collections 
        /// </summary>
        [Serializable]
        public class BusinessRelatedCollections
        {
            private BLProduct containerObject = null;
            private Dictionary<string, List<string>> RelatedCollectionsMappings = new Dictionary<string, List<string>>();

            private BLBillsDetailCollection f_CollectionBillsDetailByProduct_ID = null;
            
            private BLDealer_ProductCollection f_CollectionDealer_ProductByProduct_ID = null;
            
            private BLOfferCollection f_CollectionOfferByProduct_ID = null;
            
            private BLProduct_MeasureUnitCollection f_CollectionProduct_MeasureUnitByProduct_ID = null;
            
            private BLStore_ProductCollection f_CollectionStore_ProductByProduct_ID = null;

            /// <summary>
            /// returns  BLBillsDetailCollections that depend on this Product_ID property
            /// </summary>
            public BLBillsDetailCollection CollectionBillsDetailByProduct_ID
            {
                get
                {
                    if (this.f_CollectionBillsDetailByProduct_ID == null)
                    {
                        this.f_CollectionBillsDetailByProduct_ID = new BLBillsDetailCollection();
                        if (containerObject.Product_ID != null)
                        {
                            this.f_CollectionBillsDetailByProduct_ID.Template.Product_ID = containerObject.Product_ID;
                            this.f_CollectionBillsDetailByProduct_ID.LoadByProperties(new string[] { BLProduct.PropertiesNames.Product_ID });
                        }
                    }
                    return this.f_CollectionBillsDetailByProduct_ID;
                }
            }
            
            /// <summary>
            /// returns  BLDealer_ProductCollections that depend on this Product_ID property
            /// </summary>
            public BLDealer_ProductCollection CollectionDealer_ProductByProduct_ID
            {
                get
                {
                    if (this.f_CollectionDealer_ProductByProduct_ID == null)
                    {
                        this.f_CollectionDealer_ProductByProduct_ID = new BLDealer_ProductCollection();
                        if (containerObject.Product_ID != null)
                        {
                            this.f_CollectionDealer_ProductByProduct_ID.Template.Product_ID = containerObject.Product_ID;
                            this.f_CollectionDealer_ProductByProduct_ID.LoadByProperties(new string[] { BLProduct.PropertiesNames.Product_ID });
                        }
                    }
                    return this.f_CollectionDealer_ProductByProduct_ID;
                }
            }
            
            /// <summary>
            /// returns  BLOfferCollections that depend on this Product_ID property
            /// </summary>
            public BLOfferCollection CollectionOfferByProduct_ID
            {
                get
                {
                    if (this.f_CollectionOfferByProduct_ID == null)
                    {
                        this.f_CollectionOfferByProduct_ID = new BLOfferCollection();
                        if (containerObject.Product_ID != null)
                        {
                            this.f_CollectionOfferByProduct_ID.Template.Product_ID = containerObject.Product_ID;
                            this.f_CollectionOfferByProduct_ID.LoadByProperties(new string[] { BLProduct.PropertiesNames.Product_ID });
                        }
                    }
                    return this.f_CollectionOfferByProduct_ID;
                }
            }
            
            /// <summary>
            /// returns  BLProduct_MeasureUnitCollections that depend on this Product_ID property
            /// </summary>
            public BLProduct_MeasureUnitCollection CollectionProduct_MeasureUnitByProduct_ID
            {
                get
                {
                    if (this.f_CollectionProduct_MeasureUnitByProduct_ID == null)
                    {
                        this.f_CollectionProduct_MeasureUnitByProduct_ID = new BLProduct_MeasureUnitCollection();
                        if (containerObject.Product_ID != null)
                        {
                            this.f_CollectionProduct_MeasureUnitByProduct_ID.Template.Product_ID = containerObject.Product_ID;
                            this.f_CollectionProduct_MeasureUnitByProduct_ID.LoadByProperties(new string[] { BLProduct.PropertiesNames.Product_ID });
                        }
                    }
                    return this.f_CollectionProduct_MeasureUnitByProduct_ID;
                }
            }
            
            /// <summary>
            /// returns  BLStore_ProductCollections that depend on this Product_ID property
            /// </summary>
            public BLStore_ProductCollection CollectionStore_ProductByProduct_ID
            {
                get
                {
                    if (this.f_CollectionStore_ProductByProduct_ID == null)
                    {
                        this.f_CollectionStore_ProductByProduct_ID = new BLStore_ProductCollection();
                        if (containerObject.Product_ID != null)
                        {
                            this.f_CollectionStore_ProductByProduct_ID.Template.Product_ID = containerObject.Product_ID;
                            this.f_CollectionStore_ProductByProduct_ID.LoadByProperties(new string[] { BLProduct.PropertiesNames.Product_ID });
                        }
                    }
                    return this.f_CollectionStore_ProductByProduct_ID;
                }
            }


            public BusinessRelatedCollections(BLProduct item)
            {
                this.containerObject = item;
                if (this.containerObject != null)
                {
                    this.containerObject.PropertyChanged += new PropertyChangedEventHandler(this.containerObject_PropertyChanged);
                    this.containerObject.AfterLoad += new EventHandler(this.containerObject_AfterLoad);
                }
                if (!RelatedCollectionsMappings.ContainsKey(BLProduct.PropertiesNames.Product_ID))
                {
                    this.RelatedCollectionsMappings.Add(BLProduct.PropertiesNames.Product_ID, new List<string>());
                }
                this.RelatedCollectionsMappings[BLProduct.PropertiesNames.Product_ID].Add("f_CollectionBillsDetailByProduct_ID");
                
                if (!RelatedCollectionsMappings.ContainsKey(BLProduct.PropertiesNames.Product_ID))
                {
                    this.RelatedCollectionsMappings.Add(BLProduct.PropertiesNames.Product_ID, new List<string>());
                }
                this.RelatedCollectionsMappings[BLProduct.PropertiesNames.Product_ID].Add("f_CollectionDealer_ProductByProduct_ID");
                
                if (!RelatedCollectionsMappings.ContainsKey(BLProduct.PropertiesNames.Product_ID))
                {
                    this.RelatedCollectionsMappings.Add(BLProduct.PropertiesNames.Product_ID, new List<string>());
                }
                this.RelatedCollectionsMappings[BLProduct.PropertiesNames.Product_ID].Add("f_CollectionOfferByProduct_ID");
                
                if (!RelatedCollectionsMappings.ContainsKey(BLProduct.PropertiesNames.Product_ID))
                {
                    this.RelatedCollectionsMappings.Add(BLProduct.PropertiesNames.Product_ID, new List<string>());
                }
                this.RelatedCollectionsMappings[BLProduct.PropertiesNames.Product_ID].Add("f_CollectionProduct_MeasureUnitByProduct_ID");
                
                if (!RelatedCollectionsMappings.ContainsKey(BLProduct.PropertiesNames.Product_ID))
                {
                    this.RelatedCollectionsMappings.Add(BLProduct.PropertiesNames.Product_ID, new List<string>());
                }
                this.RelatedCollectionsMappings[BLProduct.PropertiesNames.Product_ID].Add("f_CollectionStore_ProductByProduct_ID");
            }

            public void ResetProperties(string[] propertiesNames)
            {
                for (int i = 0; i < propertiesNames.Length; i++)
                {
                    if (this.RelatedCollectionsMappings.ContainsKey(propertiesNames[i]))
                    {
                        for (int j = 0; j < this.RelatedCollectionsMappings[propertiesNames[i]].Count; j++)
                        {
                            string FieldName = this.RelatedCollectionsMappings[propertiesNames[i]][j];
                            this.GetType().GetField(FieldName, BindingFlags.NonPublic).SetValue(this, null);
                        }
                    }
                }
            }


            private void containerObject_PropertyChanged(object sender, PropertyChangedEventArgs e)
            {
                this.ResetProperties(new string[] { e.PropertyName });
            }
            private void containerObject_AfterLoad(object sender, EventArgs e)
            {
                string[] propertiesNames = new string[this.RelatedCollectionsMappings.Keys.Count];
                this.RelatedCollectionsMappings.Keys.CopyTo(propertiesNames, 0);

                this.ResetProperties(propertiesNames);
            }
        }
        #endregion

    }
}
